﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Faust.Andrew.MTG.Player
{
	/// <summary>
	/// Interaction logic for DockablePanel.xaml
	/// </summary>
    //[ContentProperty("Content")]
	public partial class DockablePanel : UserControl
	{
        public static readonly DependencyProperty TitleProperty = DependencyProperty.Register("Title", typeof(String), typeof(DockablePanel), new UIPropertyMetadata(String.Empty, new PropertyChangedCallback(OnTitleChanged), new CoerceValueCallback(OnCoerceTitle)));

        private static object OnCoerceTitle(DependencyObject o, object value)
        {
            DockablePanel dockablePanel = o as DockablePanel;
            if (dockablePanel != null)
                return dockablePanel.OnCoerceTitle((String)value);
            else
                return value;
        }

        private static void OnTitleChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
        {
            DockablePanel dockablePanel = o as DockablePanel;
            if (dockablePanel != null)
                dockablePanel.OnTitleChanged((String)e.OldValue, (String)e.NewValue);
        }

        protected virtual String OnCoerceTitle(String value)
        {
            // TODO: Keep the proposed value within the desired range.
            return value;
        }

        protected virtual void OnTitleChanged(String oldValue, String newValue)
        {
            // TODO: Add your property changed side-effects. Descendants can override as well.
        }

        public String Title
        {
            // IMPORTANT: To maintain parity between setting a property in XAML and procedural code, do not touch the getter and setter inside this dependency property!
            get
            {
                return (String)GetValue(TitleProperty);
            }
            set
            {
                SetValue(TitleProperty, value);
            }
        }
        
		public DockablePanel()
		{
			this.InitializeComponent();
		}

		private void btnDetach_Click(object sender, System.Windows.RoutedEventArgs e)
		{
            if (Content != null)
            {
                object content = Content;
                Content = null;

                Visibility = Visibility.Collapsed;

                FloatingWindow floatingWindow = new FloatingWindow(content, this);
                floatingWindow.Title = Title;
                floatingWindow.Show();
            }
		}

        public void Attach(object content)
        {
            if (content != null)
            {
                Content = content;
                Visibility = Visibility.Visible;
            }
            
        }

        private Point _startingPoint;
        private Point _startingSize;
        private bool _isResizing;
        private FrameworkElement _capturingElement;

        private void Grip_PreviewMouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            FrameworkElement element = sender as FrameworkElement;

            if (element == null) { return; }
            e.Handled = true;

            _startingPoint = e.GetPosition(null);
            _startingSize = new Point(this.ActualWidth, this.ActualHeight);
            _isResizing = true;
            _capturingElement = element;
            _capturingElement.CaptureMouse();  
        }

        private void Grip_PreviewMouseMove(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (!_isResizing) { return; }

            e.Handled = true;
            Point location = e.GetPosition(null);
            Point size = new Point();
            size.X = _startingSize.X + (location.X - _startingPoint.X);
            size.Y = _startingSize.Y + (location.Y - _startingPoint.Y);

            if (size.X < 10) { size.X = 10; }
            if (size.Y < 10) { size.Y = 10; }

            this.Width = size.X;
            this.Height = size.Y;
        }

        private void Grip_PreviewMouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (_capturingElement != null)
            {
                _capturingElement.ReleaseMouseCapture();
            }

            _isResizing = false;
            _capturingElement = null;
            e.Handled = true;
        }
	}
}